//
//  XWAudioViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/12.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWAudioViewController.h"
#import "HeadHelp.h"
#import "XWUrl.h"
#import "XWSongListViewController.h"
#import "XWMusicModel.h"
#import "XWMusicCollectionViewCell.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "XWNetWorkTool.h"


@interface XWAudioViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) NSMutableArray *songMenuArrayM;
@property (nonatomic ,strong) UICollectionView *songMenuCollectionView;
@property (nonatomic ,assign) NSInteger songMenuPage;

@end

@implementation XWAudioViewController

static NSString *reuseId = @"songMenu";
#pragma mark - setterArray
- (NSMutableArray *)songMenuArrayM
{
    if (!_songMenuArrayM) {
        _songMenuArrayM = [NSMutableArray array];
        self.songMenuPage = 1;
        [self loadSongMenuWithPage:self.songMenuPage array:self.songMenuArrayM reloadView:self.songMenuCollectionView];
    }
    return _songMenuArrayM;
}
#pragma mark - viewAppear - > viewLoad
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.songMenuCollectionView.backgroundColor = [UIColor whiteColor];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"musicTitle"]];
    [self setUpSongMenuCollectionView];
    [self setUpRefreshFooter];


}

#pragma mark - setUpCollectionView
- (void)setUpSongMenuCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.songMenuCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.minimumInteritemSpacing = 10;
    [self.songMenuCollectionView registerClass:[XWMusicCollectionViewCell class] forCellWithReuseIdentifier:reuseId];
    self.songMenuCollectionView.delegate = self;
    self.songMenuCollectionView.dataSource = self;
    [self.view addSubview:self.songMenuCollectionView];
}

- (void)setUpRefreshFooter
{
    __weak typeof(self) weakSelf = self;
    self.songMenuCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.songMenuPage += 1;
        [self loadSongMenuWithPage:self.songMenuPage array:weakSelf.songMenuArrayM reloadView:weakSelf.songMenuCollectionView];
        self.songMenuCollectionView.mj_footer.hidden = YES;
    }];
}

#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.songMenuCollectionView.mj_footer.hidden = self.songMenuArrayM.count == 0;
    return self.songMenuArrayM.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XWMusicModel *musicTable = self.songMenuArrayM[indexPath.row];
    XWMusicCollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
    [collectionCell setSongMenu:musicTable];
    return collectionCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XWMusicModel *tables = self.songMenuArrayM[indexPath.row];
    NSLog(@"---%@",tables.listid);
    XWSongListViewController *listView = [[XWSongListViewController alloc] init];
    listView.listid = tables.listid;
    listView.pic = tables.pic_300;
    [self.navigationController pushViewController:listView animated:YES];
}
#pragma mark - layout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger width = (KScreen_Width - 30) / 2;
    NSLog(@"%ld",(long)width);
    CGSize size = CGSizeMake(width, width + 40);
    return size;
}
#pragma mark - loadData

- (void)loadSongMenuWithPage:(NSInteger)page array:(NSMutableArray *)array reloadView:(UICollectionView *)view
{
    [XWNetWorkTool netWorkToolGetWithUrl:XWUrl parameters:XWParams(@"method":@"baidu.ting.diy.gedan",@"page_no":[NSString stringWithFormat:@"%ld",page],@"page_size":@"30") response:^(id response) {
        for (NSDictionary *dict in response[@"content"]) {
            XWMusicModel *tables = [XWMusicModel mj_objectWithKeyValues:dict];
            [array addObject:tables];
            [view reloadData];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
